
package com.ruipengkj.view.giftView;
/**
 * 说明：
 * 添加礼品信息界面
 * @Author：孙航
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TGift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TGiftService;
import com.ruipengkj.view.base.BaseDialog;


public class GiftDialog extends BaseDialog {

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent giftID;
	private JLabelJTextFieldComponent commodityID;
	private JLabelJTextFieldComponent money;
//	private JLabelJComboBoxComponent manager;//管理员
	private JLabelJTextFieldComponent type;
	private JLabelJTextFieldComponent value;
	
	private JButton sureButton;
	private JButton cancelButton;
	
	private TGift tGift = null;
	private TGiftService giftService;
//	private TEmployeeService tEmployeeService;//
	public boolean isSuccess=false;//判断是否添加成功
	public GiftDialog(JFrame jf,String title,boolean b) {
		super(jf, title, b);
		giftService=BuildSpringApplication.getInstance().getTGiftServiceImpl();
//		tEmployeeService=BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		this.setSize(350, 400);
		this.init();
		this.initListener();
		this.setLocationRelativeTo(null);
	}
	
	public void init() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		giftID= new JLabelJTextFieldComponent("  *", "礼品编号：", 30);
		commodityID = new JLabelJTextFieldComponent("  *", "商品编号：", 30);
		money = new JLabelJTextFieldComponent("  *", "赠送金额：", 30);
//		manager = new JLabelJComboBoxComponent("*", "仓库管理员：",new String[]{},188);
//		manager.addElement(new IDNameBean("0", "请选择"));
		type= new JLabelJTextFieldComponent("*", "赠送类型：", 15);
		value = new JLabelJTextFieldComponent("*", "上阀：", 15);
		
		giftID.getText().setText(UUID.GenUUID15());
		giftID.getText().setEditable(false);
		type.getText().setText("0");
		
		main_jp.add(giftID);
		main_jp.add(commodityID);
		main_jp.add(money);
//		main_jp.add(manager);
		main_jp.add(type);
		main_jp.add(value);
		
		JPanel p1 = new JPanel();
		sureButton=new JButton("保存(Ctrl+S)");
		cancelButton=new JButton("取消(Ctrl+Q)");
		p1.add(sureButton);
		p1.add(cancelButton);
		main_jp.add(p1);
		this.add(main_jp);
	}

	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			String giftIDStr=giftID.getText().getText().trim();
			String commodityIDStr=commodityID.getText().getText().trim();
			String moneyStr=money.getText().getText().trim();
//			Object managerObj=manager.getBox().getSelectedItem();
			String typeStr=type.getText().getText().trim();
			String valueStr=value.getText().getText().trim();
			if(commodityIDStr.equals("")){
				JOptionPane.showMessageDialog(null, "商品编号不能为空");
				commodityID.getText().requestFocus();
				return;
			}
			if(moneyStr.equals("")){
				JOptionPane.showMessageDialog(null, "赠送金额不能为空");
				money.getText().requestFocus();
				return;
			}
			if(valueStr.equals("")){
				JOptionPane.showMessageDialog(null, "上阀不能为空");
				value.getText().requestFocus();
				return;
			}
			if(typeStr.equals("")){
				JOptionPane.showMessageDialog(null, "赠送类型不能为空");
				type.getText().requestFocus();
				return;
			}
//			IDNameBean bean=(IDNameBean)managerObj;
//			if(bean.getStrId().equals("0")){
//				JOptionPane.showMessageDialog(null, "请先选择仓库管理员");
//				manager.getBox().requestFocus();
//				return;
//			}
			
			
			boolean isOpLog=false;
			if(tGift==null){
				tGift = new TGift();
				isOpLog=true;
			}
			tGift.setGiftID(giftIDStr);
			tGift.setCommodityID(commodityIDStr);
			tGift.setMoney(Double.parseDouble(moneyStr));
//			tStore.setManager(bean.getStrId());
			tGift.setType(Byte.parseByte(typeStr));
			tGift.setValve(Double.parseDouble(valueStr));
			try {
				this.giftService.saveOrUpdateService(tGift);
				this.isSuccess=true;
				if(isOpLog){
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加礼品信息");
				}else{
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改礼品信息");	
				}
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == cancelButton) {
			this.isSuccess=false;
			this.dispose();
		}
	}
	
	public void showDialog(TGift tGift){
		this.isSuccess=false;
//		SwingUtilities.invokeLater(queryEmp);
		if(tGift!=null){
			this.tGift=tGift;
			giftID.getText().setText(tGift.getGiftID());
			commodityID .getText().setText(tGift.getCommodityID());
			money.getText().setText(Double.toString(tGift.getMoney()));
//			manager.getText().setText(tStore.getManager());
			type.getText().setText(tGift.getType()+"");
			value.getText().setText(Double.toString(tGift.getValve()));
		}
		this.setVisible(true);
	}

//	public Runnable queryEmp=new Runnable() {
//		@Override
//		public void run() {
//			try {
//				List<TEmployee> list=tEmployeeService.findService(null);
//				if(list!=null&&list.size()!=0){
//					for(int i=0;i<list.size();i++){
//						TEmployee emp=list.get(i);
//						manager.addElement(new IDNameBean(emp.getEmployeeID(), emp.getName()));
//						if(tStore!=null){
//							if(emp.getEmployeeID().equals(tStore.getManager())){
//								manager.getBox().setSelectedIndex(i+1);
//							}
//						}
//					}
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//	};
}
